
package com.diy.sigmund.diycommon.designpatterns.state;

/**
 * @author ylm-sigmund
 * @since 2022/2/15 17:50
 */
public class WinnerState implements State {

    GumballMachine gumballMachine;

    public WinnerState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarterState() {
        System.out.println("请稍后，我们马上给你一颗糖果");
    }

    @Override
    public void ejectQuarterState() {
        System.out.println("抱歉，你已经转过曲柄");
    }

    @Override
    public void turnCrank() {
        System.out.println("不会因为转两次就拿到两次糖果");
    }

    @Override
    public void dispense() {
        gumballMachine.releaseBall();
        if (gumballMachine.getCount() > 0) {
            gumballMachine.releaseBall();
            if (gumballMachine.getCount() > 0) {
                gumballMachine.setState(gumballMachine.getNoQuarterState());
            } else {
                System.out.println("糖果已售罄");
                gumballMachine.setState(gumballMachine.getSoldOutState());
            }
        } else {
            gumballMachine.setState(gumballMachine.getSoldOutState());
        }
    }
}
